<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共（函数）文件
if (!function_exists('p')) {
    /**
     * @param $data  格式化打印数据
     */
    function p($data){
        header("Content-type:text/html;charset=utf-8");
        // 定义样式
        $str='<pre style="display: block;padding: 9.5px;margin: 44px 0 0 0;font-size: 13px;line-height: 1.42857;color: #333;word-break: break-all;word-wrap: break-word;background-color: #F5F5F5;border: 1px solid #CCC;border-radius: 4px;">';
        // 如果是boolean或者null直接显示文字；否则print
        if (is_bool($data)) {
            $show_data=$data ? 'true' : 'false';
        }elseif (is_null($data)) {
            $show_data='null';
        }else{
            $show_data=print_r($data,true);
        }
        $str.=$show_data;
        $str.='</pre>';
        echo $str;
    }
}

if (!function_exists('encrypt_password')) {
    /**
     * 密码加密方法
     * @param string $pw 要加密的字符串
     * @return string
     */
    function encrypt_password($pw,$authcode='www.mgchen.com'){
        return md5(md5(md5($authcode . $pw)));
    }

}

if (!function_exists('time_format')) {
    /*
    ** 格式化时间
     * @param  [String]  $time [需要格式化的时间戳]
     * @return [String]         [返回一个字符串]
    */
    function time_format($time){
        //获取当前时间
        $now = time();
        //今天零时零分零秒
        $today = strtotime(date('y-m-d',$now));
        //当前时间与传递时间相差的秒数
        $diff = $now - $time;
        $str = '';
        switch ($time) {
            case $diff < 60 :
                $str = $diff . '秒前';
                break;
            case $diff < 3600 :
                $str = floor($diff / 60) . '分钟前';
                break;
            case $diff < (3600 * 8) :
                $str = floor($diff / 3600) . '小时前';
                break;
            case $time > $today :
                $str = '今天&nbsp;&nbsp;' . date('H:i',$time);
                break;
            default:
                $str = date('Y-m-d H:i:s',$time);
                break;
        }
        return $str;

    }
}


if (!function_exists('replace_weibo')) {
    /**
     * 替换微博内容的URL地址、@用户与表情
     * @param  [String] $content [需要处理的微博字符串]
     * @return [String]          [处理完成后的字符串]
     */
    function replace_weibo($content){
        if (empty($content))  return false;
        //给URL地址加上 <a> 链接
        $preg = '/(?:http:\/\/)?([\w.]+[\w\/]*\.[\w.]+[\w\/]*\??[\w=\&\+\%]*)/is';
        $content = preg_replace($preg, '<a href="http://\\1" target="_blank">\\1</a>', $content);

        //给@用户加是 <a> 链接
        $preg = '/@(\S+)\s/is';
        $content = preg_replace($preg, '<a href="/User/\\1">@\\1</a>', $content);

        //提取微博内容中所有表情文件
        $preg = '/\[(\S+?)\]/is';
        preg_match_all($preg, $content, $arr);
        //载入表情库
        $phiz = include './data/phiz.php';
        $src = "/static/home/images/phiz/";
        $suffix = ".gif";
        if (!empty($arr[1])) {
            foreach ($arr[1] as $k => $v) {
                //搜索值对应的键名
                $name = array_search($v,$phiz);
                if  ($name) {
                    $content = str_replace($arr[0][$k],"<img src='{$src}{$name}{$suffix}'  title='". $v ."' />",$content);
                }
            }
        }
        return $content;

    }
}


if (!function_exists('set_msg')) {
    /**
     * [往内存写入推送消息]
     * @param [int] $uid  [用户ID]
     * @param [int] $type [1:评论; 2:私信; 3: @用户]
     * @param [boolean] $flush  [是否清0]
     */
    function set_msg ($uid,$type,$flush=false) {
        $name = '';
        switch ($type) {
            case 1 :
                $name = 'comment';
                break;

            case 2 :
                $name = 'letter';
                break;

            case 3 :
                $name = 'atme';
                break;
        }
        if ($flush) {
            $data = cache('usermsg' . $uid);
            $data[$name]['total'] = 0;
            $data[$name]['status'] = 0;
            cache('usermsg' . $uid, $data, 0);
            return;
        }

        if (cache('usermsg' . $uid)) {
            //内存数据存在时，让相应数据+1
            $data = cache('usermsg' . $uid);
            $data[$name]['total']++;
            $data[$name]['status'] = 1;
            cache('usermsg' . $uid, $data, 0);
        } else {
            //内存数据不存在时，初始化数据并且写入内存
            $data = array(
                'comment' => array('total' => 0 , 'status' => 0),
                'letter' => array('total' => 0 , 'status' => 0),
                'atme' => array('total' => 0 , 'status' => 0)
            );
            $data[$name]['total']++;
            $data[$name]['status'] = 1;
            cache('usermsg' . $uid, $data, 0);
        }
    }
}

